//
//  NSDate+LLDescriptionString.m
//  live.ly
//
//  Created by CuiHao on 9/27/16.
//  Copyright © 2016 musical.ly. All rights reserved.
//

#import "NSDate+LLDescriptionString.h"


@implementation NSDate (LLDescriptionString)

- (int)hoursFromDate:(NSDate *)updateTime
{
    if (updateTime) {
        NSTimeInterval interval = [[NSDate new] timeIntervalSinceDate:updateTime];
        return (int)interval / (60 * 60);
    }
    return 0;
}

- (NSString *)timeToNowString
{
    int hours = [self hoursFromDate:self];
    if (hours > 0) {
        return [NSString stringWithFormat:NSLocalizedString(@"%dh ago", nil), hours];
    } else {
        return NSLocalizedString(@"just now", nil);
    }
}

- (NSString *)timeToNowInChannelString
{
    int hours = [self hoursFromDate:self];
    if (hours > 0) {
        return [NSString stringWithFormat:NSLocalizedString(@"%dh", nil), hours];
    } else {
        return NSLocalizedString(@"just now", nil);
    }
}

+ (NSString *)timestampToStringWithDateFormat:(NSString *)dateFormat timestamp:(NSNumber *)timestamp
{
    NSTimeInterval seconds = [timestamp integerValue] / 1000.0;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:seconds];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormat = [NSDateFormatter dateFormatFromTemplate:dateFormat options:0 locale:nil];
    [dateFormatter setDateFormat:dateFormat];
    return [dateFormatter stringFromDate:date];
}

@end
